/**
 * ...
 * @author Thot
 */

function LibraryScreen()
{
}


	//static
	LibraryScreen.ON_SELECT			= "onSelect";
	LibraryScreen.ADD_ANIMATION		= "addAnimation";

	// private
	LibraryScreen.prototype._list			= null;
	LibraryScreen.prototype._renderer		= null;
	LibraryScreen.prototype._preview		= null;
	LibraryScreen.prototype._addButton		= null;
	
	
	
	// public
	LibraryScreen.prototype.signal		= null;
	

	
	
	LibraryScreen.prototype.init = function( p_list, p_preview )
	{
		var camera = new Camera();	
		var layer = new Layer();
		var canvas = p_preview;
		
		layer.name = "preview";
		layer.displayList = new DisplayList();
		layer.canvas = canvas;
		layer.context = layer.canvas.getContext("2d");
		layer.sorted = false;
		
		this.signal = new Signal();
		this._list = p_list;
		this._preview = p_preview;
		this._renderer = new Renderer2D();
		this._renderer.layers.push( layer );
		this._renderer.camera = camera;
		
		camera.width = p_preview.width;
		camera.height = p_preview.height;
		
		this._list.addEventListener( "click", CallBack.create( this._clickHandler, this ) );
	};
	
	LibraryScreen.prototype._clickHandler = function(event)
	{
		var node = event.target;
		var animId = node.innerHTML;
		this.signal.dispatchEvent( LibraryScreen.ON_SELECT, [animId] );
		this._unSelectAll();
		node.setAttribute("class", "selected");
	};
	
	LibraryScreen.prototype._unSelectAll = function()
	{
		var link = null;
		var linkList = this._list.getElementsByTagName("a");
		var i = 0;
		var max = linkList.length;
		
		for( ; i < max; i++ )
		{
			link = linkList[i];
			link.setAttribute( "class", "" );
		}
	};
	
	LibraryScreen.prototype.renderAnimation = function( p_animVO )
	{
		var anim = AnimationFactory.getAnimationFromVO( p_animVO );
		var spr = new Sprite();
		var layer = this._renderer.layers[0];
		var ratio = p_animVO.height / this._preview.height;
		
		spr.width = p_animVO.width / ratio;
		spr.height = this._preview.height;
		spr.x = ( this._preview.width - spr.width ) * 0.5;
		
		spr.uv 			= anim.data[0].uv;
		spr.textureId 	= anim.data[0].tex;
		
		layer.displayList.removeChildren();
		layer.displayList.addChild(spr);
		this._renderer.render();
	};
	
	
	LibraryScreen.prototype.addAnimation = function( p_animId )
	{
		var li = document.createElement("li");
		var link = document.createElement("a");
		
		var className = ( this._list.children.length % 2 == 0 ) ? "impair" : "pair";
		link.setAttribute( "class", className );
		link.setAttribute( "href", "#" );
		link.innerHTML = p_animId;
		
		li.appendChild( link );
		this._list.appendChild( li );
	};
	
	LibraryScreen.prototype.clean = function()
	{
		var layer = this._renderer.layers[0];
		layer.displayList.removeChildren();
		this._renderer.render();
		
		while( this._list.children.length > 0 )
		{
			this._list.removeChild( this._list.children[0] );
		}
	};
	
	
